from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
# 初始化驱动
driver = webdriver.Chrome()
# 打开126邮箱网页
driver.get("https://mail.126.com/")
# 最大化
driver.maximize_window()
# 设置隐式等待
driver.implicitly_wait(15)
# 跳进iframe
iframes = WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.TAG_NAME,"iframe")))
driver.switch_to.frame(iframes[0])
# 输入账号
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[@data-loginname="loginEmail"]'))).send_keys("baijiacheng_1231")
# WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.NAME,"email"))).send_keys("baijiacheng_1231")
# WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"j-inputtext")))[0].send_keys("baijiacheng_1231")
# 输入密码
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"j-inputtext")))[1].send_keys("qwe123..")
# 点击登录按钮
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.ID,"dologin"))).click()

# 跳出所有的iframe内联框架
# driver.switch_to.default_content()

# 跳出当前iframe框架
driver.switch_to.parent_frame()

sleep(2)
# 点击未读邮件
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.CLASS_NAME,"ki1")))[0].click()


# 等待
sleep(10)
"""
定位元素找不到的几种原因：
1、定位元素有问题，换一个再看看
2、等待时间没有给，页面还没加载出来，元素操作失败
3、没有进行句柄切换，导致页面定位元素还在第一个窗口里
4、出现了iframe内联框架，这个相当于网页嵌套了一个网页，需要先跳进才来处理
"""